package com.seeyon.apps.dzdd.event;

import com.seeyon.apps.collaboration.event.CollaborationFinishEvent;
import com.seeyon.apps.collaboration.event.CollaborationStartEvent;
import com.seeyon.apps.collaboration.po.ColSummary;
import com.seeyon.apps.dzdd.kit.FormCap4Kit;
import com.seeyon.apps.dzdd.kit.StrKit;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormDataSubBean;
import com.seeyon.cap4.form.service.CAP4FormManager;
import com.seeyon.cap4.template.manager.CAPDataListSqlHelpManager;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.util.annotation.ListenEvent;
import org.apache.commons.logging.Log;

import java.util.List;
import java.util.Map;

public class CustomCollaborationEventListener {

    private static final Log log = CtpLogFactory.getLog(CustomCollaborationEventListener.class);

    private CAP4FormManager cap4FormManager = (CAP4FormManager) AppContext.getBean("cap4FormManager");
    private CAPDataListSqlHelpManager capDataListSqlHelpManager = (CAPDataListSqlHelpManager) AppContext.getBean("capDataListSqlHelpManager");

    /**
     * 监听协作开始事件
     * 当协作开始时，此方法会被触发，主要用于处理特定模板的表单数据
     * 它尝试从事件中获取摘要信息，并根据模板代码执行特定的逻辑
     *
     * @param event 协作开始事件，包含表单摘要和模板代码等信息
     */
    @ListenEvent(event = CollaborationFinishEvent.class)
    public void onCollaborationFinishEvent(CollaborationStartEvent event) {
        try {
            log.info("进入流程结束监听事件");
            // 获取事件摘要
            ColSummary colSummary = event.getSummary();
            // 根据表单应用ID获取表单Bean
            FormBean formBean = cap4FormManager.getForm(colSummary.getFormAppid(), true);
            // 检查模板代码是否为特定值，以决定是否继续处理
            if (event.getTemplateCode().equals("ZCPDJH_001") || event.getTemplateCode().equals("ZCPDJH_002")) {
                log.info("进入资产盘点计划流程结束监听事件");
                // 获取表单主数据Bean
                FormDataMasterBean formDataMasterBean = cap4FormManager.getDataMasterBeanById(colSummary.getFormRecordid(), formBean, null);
                String subTableName = formBean.getFieldBeanByDisplay("资产部门").getOwnerTableName();
                List<FormDataSubBean> formDataSubBeans = formDataMasterBean.getSubData(subTableName);

                /*资产卡片*/
                FormBean zckp_formBean = cap4FormManager.getFormByFormCode("ZCKP_001");
                String sybm_FieldName = StrKit.str(zckp_formBean.getFieldBeanByDisplay("使用部门-W").getName());

                /*资产盘点情况表*/
                FormBean pdqkb_formBean = cap4FormManager.getFormByFormCode("ZCPDQKB_001");

                String zckp_tableName = zckp_formBean.getMasterTableBean().getTableName();
                String jhr = str(formDataMasterBean.getFieldValue(formBean.getFieldBeanByDisplay("计划人").getName()));
                String jhxdrq = str(formDataMasterBean.getFieldValue(formBean.getFieldBeanByDisplay("计划下达日期").getName()));
                for (FormDataSubBean formDataSubBean : formDataSubBeans) {
                    String planNumber = str(formDataSubBean.getFieldValue(formBean.getFieldBeanByDisplay("盘点工作编号").getName()));
                    String pdr = str(formDataSubBean.getFieldValue(formBean.getFieldBeanByDisplay("盘点人").getName()));
                    String jpr = str(formDataSubBean.getFieldValue(formBean.getFieldBeanByDisplay("监盘人").getName()));
                    String deptId = str(formDataSubBean.getFieldValue(formBean.getFieldBeanByDisplay("资产部门").getName()));
                    StringBuilder zckp_stb = new StringBuilder();
                    zckp_stb.append("select * from ").append(zckp_tableName).append(" where ");
                    zckp_stb.append(sybm_FieldName).append(" = '").append(deptId).append("'");
                    List<Object> dataList = capDataListSqlHelpManager.selectDataList(zckp_stb.toString(), null);
                    if (!dataList.isEmpty()) {
                        for (int i = 0; i < dataList.size(); i++) {
                            Map<String, Object> dataMap = (Map<String, Object>) dataList.get(i);
                            FormDataMasterBean pdqk_formDataMasterBean = FormDataMasterBean.newInstance(pdqkb_formBean);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("盘点工作编号").getName(), planNumber);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("资产名称").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("资产名称").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("规格型号").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("规格型号").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("产权单位").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("产权单位").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("资产编码").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("资产编码").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("EAS编码").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("EAS编码").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("资产类别").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("资产类别").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("资产状态").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("资产状态").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("账务入账日期").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("入库日期").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("存放位置").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("存放位置").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("责任部门").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("使用部门-W").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("使用保管人").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("使用保管人-W").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("备注").getName(), dataMap.get(zckp_formBean.getFieldBeanByDisplay("备注").getName()));
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("是否更新资产卡片").getName(), -5262438096267164683L);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("盘点情况").getName(), 7353112567001140212L);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("盘点计划下达人").getName(), jhr);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("盘点人").getName(), pdr);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("计划下达日期").getName(), jhxdrq);
                            pdqk_formDataMasterBean.addFieldValue(pdqkb_formBean.getFieldBeanByDisplay("监盘人").getName(), jpr);
                            FormCap4Kit.addUnFlowFormDataCap4(pdqk_formDataMasterBean, pdqkb_formBean);
                        }
                    }
                }
            }
        } catch (Exception e) {
            // 日志记录任何在处理过程中发生的异常
            log.error("资产盘点计划下达将资产盘点信息写入资产盘点情况表中异常信息:", e);
        }
    }

    /**
     * 将给定对象转换为字符串
     * 如果对象为null，返回空字符串；如果对象是字符串类型，直接返回；
     * 否则，返回对象的toString()方法的结果
     *
     * @param o 要转换为字符串的对象
     * @return 对象的字符串表示，或空字符串（如果对象为null）
     */
    private static String str(Object o) {
        // 检查对象是否为null，是则返回空字符串
        if (o == null) {
            return "";
        }
        // 如果对象本身就是字符串，直接返回该字符串
        if (o instanceof String) {
            return (String) o;
        }
        // 对于其他情况，返回对象的toString()结果
        return o.toString();
    }

}
